package com.vinaprime.primeschedule.businesslayer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPEvent_DL;
import com.vinaprime.primeschedule.entities.VPEvent;
import com.vinaprime.primeschedule.entities.VPEvent;

@SuppressLint("DefaultLocale")
public class VPEvent_BL {
	private VPEvent_DL myEventDL;
	private SimpleDateFormat myDBDateFormat;
	private java.text.DateFormat myDateFormat;
	public VPEvent_BL (Context context, String path){
		this.myEventDL = new VPEvent_DL(context, path);
		this.myDBDateFormat =  new SimpleDateFormat(VPEvent_DL.getDbDateformat());
		this.myDateFormat = SimpleDateFormat.getDateInstance();
	}
	public int getNewEventId (){
		
		int result = 0;
		Cursor cur =  myEventDL.getNewEventID();
		if (cur.moveToFirst())
		{
			if (!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeId()))) {
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeId())));
			}
		}
		return result+1;
	}
	
	public List<VPEvent> getEventbyDate(Date myDate) throws IllegalArgumentException, ParseException{
		List<VPEvent> listEvent = new ArrayList<VPEvent>();
		Date startdate, enddate;
		Cursor cur  = this.myEventDL.getEventbyDate(myDate);
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
				VPEvent myEvent = new VPEvent();
				myEvent.setVpeId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeId()))));
				myEvent.setVpeTitle(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeTitle())));
				myEvent.setVpeAddress(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAdress())));
				myEvent.setVpeLocation(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeLocation())));				
				myEvent.setVpeRepeatSchedule(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeRepeatschedule())));
				myEvent.setVpeAlarm(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAlarm())));
				myEvent.setVpeURL(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeUrl())));
				myEvent.setVpeAvailable(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAvailable())));
				myEvent.setVpeDescription(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeDescription())));
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())))
				{
					startdate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())));
					myEvent.setVpeStartDate(this.myDateFormat.parse(this.myDateFormat.format(startdate)));
				}
				
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())))
				{
					enddate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())));
					myEvent.setVpeEndDate(this.myDateFormat.parse(this.myDateFormat.format(enddate)));
				}		
				
				listEvent.add(myEvent);
				cur.moveToNext();
			}
		}
		return listEvent;
	}
	public VPEvent getEventbyID (int Id) throws IllegalArgumentException, ParseException{
		VPEvent myEvent = new VPEvent();
		Date startdate, enddate;
		Cursor cur  =  this.myEventDL.getEventbyId(Id);
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{			
				
				myEvent.setVpeId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeId()))));
				myEvent.setVpeTitle(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeTitle())));
				myEvent.setVpeAddress(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAdress())));
				myEvent.setVpeLocation(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeLocation())));				
				myEvent.setVpeRepeatSchedule(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeRepeatschedule())));
				myEvent.setVpeAlarm(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAlarm())));
				myEvent.setVpeURL(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeUrl())));
				myEvent.setVpeAvailable(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAvailable())));
				myEvent.setVpeDescription(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeDescription())));
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())))
				{
					startdate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())));
					myEvent.setVpeStartDate(this.myDateFormat.parse(this.myDateFormat.format(startdate)));
				}
				
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())))
				{
					enddate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())));
					myEvent.setVpeEndDate(this.myDateFormat.parse(this.myDateFormat.format(enddate)));
				}		
				
				cur.moveToNext();
			}
		}
		return myEvent;		
	}
	
	public List<VPEvent> getAllEvent() throws IllegalArgumentException, ParseException
	{
		List<VPEvent> listEvent = new ArrayList<VPEvent>();
		Date startdate, enddate;
		Cursor cur  = this.myEventDL.getAllEvent();
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
				VPEvent myEvent = new VPEvent();
				myEvent.setVpeId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeId()))));
				myEvent.setVpeTitle(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeTitle())));
				myEvent.setVpeAddress(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAdress())));
				myEvent.setVpeLocation(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeLocation())));				
				myEvent.setVpeRepeatSchedule(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeRepeatschedule())));
				myEvent.setVpeAlarm(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAlarm())));
				myEvent.setVpeURL(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeUrl())));
				myEvent.setVpeAvailable(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeAvailable())));
				myEvent.setVpeDescription(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeDescription())));
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())))
				{
					startdate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeStartdate())));
					myEvent.setVpeStartDate(this.myDateFormat.parse(this.myDateFormat.format(startdate)));
				}
				
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())))
				{
					enddate = this.myDBDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPEvent_DL.getDbTableVpeEnddate())));
					myEvent.setVpeEndDate(this.myDateFormat.parse(this.myDateFormat.format(enddate)));
				}		
				
				listEvent.add(myEvent);
				cur.moveToNext();
			}
		}
		return listEvent;
	}
	public Cursor getAllEventCursor(){
		Cursor cur  =  this.myEventDL.getAllEvent();
		return cur;
	}	
	public void addEvent(VPEvent myEvent){
		this.myEventDL.AddEvent(myEvent);
	}
	public void updateEvent(VPEvent myEvent){
		this.myEventDL.updateEvent(myEvent);
	}
	public void deleteEvent(VPEvent myEvent){
		this.myEventDL.deleteEvent(myEvent);
	}
	public void deleteEventbyID(int _Vpc_Id){
		this.myEventDL.deleteEventbyID(_Vpc_Id);
	}

	
	@SuppressLint("DefaultLocale")
	public List<VPEvent> getEventInContains(String _contains, List<VPEvent> _listEvent) throws IllegalArgumentException, ParseException
	{
		
		
		if(_contains =="" ||_contains ==null )
		{
			return _listEvent;
		}
		else
		{
			List<VPEvent> listEvent = new ArrayList<VPEvent>();
			
			int rowcount = _listEvent.size();
			
			for (int i=0;i<rowcount;i++)
				{	
					String _str = _listEvent.get(i).getVpeTitle() +" "+_listEvent.get(i).getVpeAddress();
					if (_str.toLowerCase().contains(_contains.toLowerCase()))
					{
						VPEvent myEvent = new VPEvent();
						myEvent = _listEvent.get(i);
						listEvent.add(myEvent);
						
					}
			
				}
			return listEvent;
		}
		
	}
}
